package edu.isidis.esiag.mock.DabaBaseMock;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.ItemSelectable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import com.esiag.pdsteam.referencement.bean.Message;
import com.esiag.pdsteam.referencement.bean.Source;
import com.esiag.pdsteam.referencement.dao.DataBaseAccess;

public class AccessDataSwing extends JFrame implements ItemListener, ActionListener{
	Message message;
	Source source;
	JPanel center=new JPanel();
	JPanel north=new JPanel();
	JPanel east=new JPanel();
	JLabel centerLabel=new JLabel();
	JPanel jListPanel= new JPanel(new BorderLayout());
	
	JPanel messagePanel = new JPanel(new FlowLayout());
	JComboBox messageComboBox;
	JPanel sourcePanel = new JPanel(new FlowLayout());
	JComboBox sourceComboBox;
	JPanel criticitePanel=new JPanel(new FlowLayout());
	JTextField criticiteTextField=new JTextField(5);
	private DataBaseAccess dataBase;
	
	public AccessDataSwing() throws Exception {
		dataBase = new DataBaseAccess();
		this.setLayout(new BorderLayout());
		
		north.setBorder(new BevelBorder(BevelBorder.RAISED));
		center.setBorder(new BevelBorder(BevelBorder.LOWERED));
		east.setBorder(new BevelBorder(BevelBorder.RAISED));
		
		this.getContentPane().add(center,BorderLayout.CENTER);
		this.getContentPane().add(north,BorderLayout.NORTH);
		this.getContentPane().add(east,BorderLayout.EAST);
		
		
		// center
		center.setLayout(new BoxLayout(center, BoxLayout.Y_AXIS));
		centerLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		jListPanel.setBorder(new BevelBorder(BevelBorder.LOWERED));
		centerLabel.setText("Testttt");
		
		centerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
		jListPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		center.add(centerLabel);
		center.add(jListPanel);
		
		// north
		north.setLayout(new BoxLayout(north, BoxLayout.Y_AXIS));
		
		
		sourcePanel.add(new JLabel("Source :"));
		sourceComboBox=new JComboBox(dataBase.getAllSource().toArray());
		sourceComboBox.addItemListener(this);
		sourceComboBox.setSelectedIndex(0);
		source=(Source) sourceComboBox.getSelectedItem();
		sourcePanel.add(sourceComboBox);
		
		messagePanel.add(new JLabel("Message :"));
		messageComboBox=new JComboBox(dataBase.getAllMessagebySource(source.getId()).toArray());
		messageComboBox.addItemListener(this);
		messageComboBox.setSelectedIndex(0);
		message=(Message) messageComboBox.getSelectedItem();
		messagePanel.add(messageComboBox);
		
		
		
		criticitePanel.add(new JLabel("Criticite :"));
		criticitePanel.add(criticiteTextField);
		
		messagePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		sourcePanel.setAlignmentX(Component.CENTER_ALIGNMENT);
		

		north.add(sourcePanel);
		north.add(messagePanel);
		north.add(criticitePanel);
		
		
		// east
		
		east.setLayout(new BoxLayout(east, BoxLayout.Y_AXIS));
		JButton b1=new JButton("All Message");
		b1.addActionListener(this);
		JButton b2=new JButton("All Source");
		b2.addActionListener(this);
		JButton b3=new JButton("All TypeSource");
		b3.addActionListener(this);
		
		east.add(b1);
		east.add(b2);
		east.add(b3);
		this.setSize(800, 600);
		this.setVisible(true);
		
	}
	
	public static void main(String[] args) {
		try {
			new AccessDataSwing();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void itemStateChanged(ItemEvent e) {
		JComboBox comboBox=(JComboBox) e.getSource();
		if(e.getStateChange()==ItemEvent.SELECTED)
		{
			Object obj=comboBox.getSelectedItem();
			if(obj instanceof Message)
			{
				message=(Message)obj;
			}
			else
			{
				source=(Source) obj;
				messageComboBox.removeAllItems();
				try {
					int i=0;
					for(Object m:dataBase.getAllMessagebySource(source.getId()).toArray()){
						messageComboBox.addItem(m);
						if(i==0) message=(Message)m;
						i++;
					}
					messageComboBox.setSelectedIndex(0);
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
			
			try {
				//System.out.println(">>"+dataBase.getCriticite(source.getId(), message.getCode()));
				criticiteTextField.setText(dataBase.getCriticite(source.getId(), message.getCode())+"");
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String value=((JButton)arg0.getSource()).getText();
		try {
			if(value.equals("All Message"))
			jListPanel.add(new JScrollPane(new JList(dataBase.getAllMessage().toArray())));
			else if(value.equals("All Source"))
				jListPanel.add(new JScrollPane(new JList(dataBase.getAllSource().toArray())));
			else if(value.equals("All TypeSource"))
				jListPanel.add(new JScrollPane(new JList(dataBase.getAllTypeSource().toArray())));	
			setVisible(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
